var regions,area,building,floor;
$.get('mockdata/distributionDataMonitor.json').done(
    function (data) {
        regions = data;
        for(i=0;i<data.regions.length;i++){
            $("#area").append("<option>"+data.regions[i].regionName+"</option>")
        }
    });

function onAreaSelect(){
    removeBuilding();
    removeFloor();
    removeRoom();
    area = $("#area").val();
    for(i=0; i<regions.regions.length; i++){
        if(area === regions.regions[i].regionName){
            for(j=0; j<regions.regions[i].buildings.length; j++){
                $("#building").append("<option>"+regions.regions[i].buildings[j].buildingName+"</option>")
            }
        }
    }
}

function onBuildingSelect(){
    removeFloor();
    removeRoom();
    building = $("#building").val();
    for(i=0; i<regions.regions.length; i++) {

        for(j=0; j<regions.regions[i].buildings.length; j++) {

            if(building === regions.regions[i].buildings[j].buildingName) {

                for(m=0; m<regions.regions[i].buildings[j].floors.length; m++) {
                    $("#floor").append("<option>"+regions.regions[i].buildings[j].floors[m].floorName+"</option>")
                }
            }
        }
    }
}

function onFloorSelect(){
    removeRoom();
    floor = $("#floor").val();
    for(i=0; i<regions.regions.length; i++) {

        for(j=0; j<regions.regions[i].buildings.length; j++) {

            for(m=0; m<regions.regions[i].buildings[j].floors.length; m++) {

                if(floor === regions.regions[i].buildings[j].floors[m].floorName) {

                    for( n=0; n<regions.regions[i].buildings[j].floors[m].Rooms.length; n++) {
                        $("#room").append("<option>"+regions.regions[i].buildings[j].floors[m].Rooms[n].roomName+"</option>")
                    }
                }
            }
        }
    }
}

function removeBuilding() {
    $("#building")[0].options.length=0;
    $("#building").append("<option>"+"----"+"</option>");
}

function removeFloor() {
    $("#floor")[0].options.length=0;
    $("#floor").append("<option>"+"----"+"</option>");
}

function removeRoom() {
    $("#room")[0].options.length=0;
    $("#room").append("<option>"+"----"+"</option>");
}